#! /usr/bin/env python

version="Alpha 1.0"

from PyQt4 import QtGui
Application=QtGui.QApplication([])

from ftllibs.FTL import CHAMPS as CHAMPS
from ftllibs.ui.ChampionsTab import ChampTab
from ftllibs.ui.BuildTab import BuildTab
from ftllibs.ui.CompareTab import CompareTab
from ftllibs.ui.MasteriesTab import MasteriesTab
from ftllibs.ui.RunesTab import RunesTab
from ftllibs.ui.RecommendedTab import RecommendedTab
import os

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow,self).__init__()
        self.setWindowTitle("For The LoLz - Version: %s - LoL Version: %s" % (version,"140"))
        self.setGeometry(30,50,1200,800)
        self.initUI(self)

    def initUI(self,MainWindow):
        self.MasterWidget=QtGui.QWidget(MainWindow)
        self.MasterLayout = QtGui.QHBoxLayout(self.MasterWidget)
        # Init Widgets
        self.Champions = ChampTab()
        self.Masteries = MasteriesTab()
        self.Runes = RunesTab()
        self.Options = QtGui.QWidget()
        self.Compare = CompareTab()
        self.Recommended = RecommendedTab(78)
        # Tabs
        self.MasterTab = QtGui.QTabWidget(MainWindow)
        self.MasterTab.setTabsClosable(True)
        self.MasterTab.setMovable(True)
        self.MasterTab.addTab(self.Champions,"Champions")
        self.MasterTab.addTab(self.Masteries,"Masteries")
        self.MasterTab.addTab(self.Runes,"Runes")
        self.MasterTab.addTab(self.Compare,"Compare")
        self.MasterTab.addTab(self.Recommended,"Recom")
        # layout stuff
        self.MasterLayout.addWidget(self.MasterTab)
        self.setCentralWidget(self.MasterWidget)
        # Menu und Status Bar
        exitAction = QtGui.QAction(QtGui.QIcon('images/icons/logout_normal_hover.png'), '&Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(QtGui.qApp.quit)
        openMasteriesTabAction = QtGui.QAction(QtGui.QIcon('images/masteries/Mastermind.png'), '&Masteries',self)
        openMasteriesTabAction.setShortcut('Ctrl+M')
        openMasteriesTabAction.setStatusTip('Opens the Masteries Tab')
        openMasteriesTabAction.triggered.connect(self.addMasteriesTab)
        openRunesTabAction = QtGui.QAction(QtGui.QIcon('images/masteries/Archmages_Savvy_Mastery.png'), '&Runes',self)
        openRunesTabAction.setShortcut('Ctrl+R')
        openRunesTabAction.setStatusTip('Opens the Runes Tab')
        openRunesTabAction.triggered.connect(self.addRunesTab)
        openOptionsTabAction = QtGui.QAction(QtGui.QIcon('images/icons/settings_normal_hover.png'), '&Options',self)
        openOptionsTabAction.setShortcut('Ctrl+R')
        openOptionsTabAction.setStatusTip('Opens the Options Tab')

        openOptionsTabAction.triggered.connect(self.addOptionsTab)
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)
        viewMenu = menubar.addMenu('&View')
        viewMenu.addAction(openMasteriesTabAction)
        viewMenu.addAction(openRunesTabAction)
        viewMenu.addAction(openOptionsTabAction)
        self.statusBar().showMessage('Welcome')
        # Signals
        self.MasterTab.tabCloseRequested[int].connect(self.tabClose)
        self.Champions.S_OpenChampBuild[int].connect(self.addBuildTab)
        
    def tabClose(self,tabnum):
        if tabnum == 0:
            pass
        else:
            if "Builds" in self.MasterTab.tabText(tabnum):
                widget = self.MasterTab.widget(tabnum)
                widget.RequestClose()
            self.MasterTab.removeTab(tabnum)

    def addMasteriesTab(self):
        if self.MasterTab.indexOf(self.Masteries) != -1:
            return
        else:
            self.MasterTab.addTab(self.Masteries,"Masteries")

    def addRunesTab(self):
        if self.MasterTab.indexOf(self.Runes) != -1:
            return
        else:
            self.MasterTab.addTab(self.Runes,"Runes")

    def addOptionsTab(self):
        if self.MasterTab.indexOf(self.Options) != -1:
            return
        else:
            self.MasterTab.addTab(self.Options,"Options")

    def addBuildTab(self,ChampID):
        found=False
        print " addBuildTab -> champid ->",ChampID
        ChampName=CHAMPS.GetChampName(ChampID)
        TabString="%s's Builds" % (ChampName)
        for i in range(0,self.MasterTab.count()):
            if self.MasterTab.tabText(i) == TabString:
                found=True
                self.MasterTab.setCurrentIndex(i)
                # TODO vieleicht TAB Widget Updaten ?
                break
        if not found:
            Build=BuildTab(ChampID)
            self.MasterTab.addTab(Build,TabString)
            self.MasterTab.setCurrentWidget(Build)


# Ze App
#os.chdir("/home/stefan/mysvn/forthelolz/")
#Application=QtGui.QApplication([])
ForTheLoLz=MainWindow()
ForTheLoLz.show()
Application.exec_()

#### eval: (e2wm:start-management)
### emacs stuff
### Local Variables: ###
### mode:python ###
### comment-column: 0 ###
### comment-start: "###"  ###
### comment-end:"###" ###
### compile-command: "/usr/bin/env python ~/mysvn/forthelolz/ForTheLoLz.py" ###
### eval: (svn-status-toggle-svn-verbose-flag) ###
### eval: (svn-status "~/mysvn/forthelolz") ###
### eval: (sit-for 1) ###
### eval: (switch-to-buffer "ForTheLoLz.py") ###
### eval: (e2wm:start-management) ###
### End: ###
